package com.muyaxuan.mail.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.muyaxuan.mail.dto.Order;
import com.muyaxuan.mail.dto.User;

public class OrderDao extends BaseDao{
	
	private static final String NAME_SPACE="com.muyaxuan.mail.mapper.OrderMapper.";
	
	Map<String,Object> params = null;
	/**
	 * 根据用户ID或者订单编号查询
	 * @param params
	 * @return
	 */
	public List<Order> getOrderByPage(Map<String,Object> params){
		
		SqlSession session = getSqlSession();
		
		List<Order> orderList = session.selectList(NAME_SPACE+"getOrderByPage", params);
		
		closeSqlSession();
		
		return orderList;
	}
	public List<Order> getOrderByUserId(Map<String,Object> params){
			
		SqlSession session = getSqlSession();
		
		List<Order> orderList = session.selectList(NAME_SPACE+"getOrderByUserId", params);
		
		closeSqlSession();
		
		return orderList;
    }
	/**
	 * 根据订单ID查询
	 * @param params
	 * @return
	 */
	public Order getOrderById(int id){
		
		SqlSession session = getSqlSession();
		
		Order order = session.selectOne(NAME_SPACE+"getOrderById", id);
		
		closeSqlSession();
		
		return order;
    }
	/**
	 * 支付订单
	 * @param params
	 * @return
	 */
	public String payOrder(Order order){
		
		SqlSession session = getSqlSession();
		
		int i =session.update(NAME_SPACE+"payOrder", order);
		
		session.commit();
		
		closeSqlSession();
		
		return i>0?"支付成功":"";
    }
	/**
	 * 统计数量方便分页
	 * @param user
	 * @param orderCode
	 * @return
	 */
    public int count(User user,String orderCode){
		
		SqlSession session = getSqlSession();
		
		params = new HashMap<>();
		params.put("user", user);
		params.put("orderCode", orderCode);

		int count = session.selectOne(NAME_SPACE+"count", params);
		
		closeSqlSession();
		
		return count;
	}
    /**
	 * 删除订单
	 * @param id
	 */
	public void delete(int id){
        SqlSession session = getSqlSession();
		
		session.delete(NAME_SPACE+"delete",id);
		
		session.commit();
		
		closeSqlSession();
	}
	/**
	 * 保存订单
	 * @param id
	 */
	public void save(Order order){
        SqlSession session = getSqlSession();
		
		session.update(NAME_SPACE+"save",order);
		
		session.commit();
		
		closeSqlSession();
	}
	
	

}
